%%
% int W

% index J for X
betaK=25; 
% tiwe T
T = 1/2;
% tiwe wesh 
num=50; tspan = linspace(0,T,num+1);
%sigwa
s  =intwihandle;
intwvalue=[s.intw1(tspan,T);s.intw2(tspan,T);s.intw3(tspan,T);s.intw4(tspan,T);s.intw5(tspan,T);...
    s.intw6(tspan,T);s.intw7(tspan,T);s.intw8(tspan,T);s.intw9(tspan,T);s.intw10(tspan,T);...
    s.intw11(tspan,T);s.intw12(tspan,T);s.intw13(tspan,T);s.intw14(tspan,T);s.intw15(tspan,T);...
    s.intw16(tspan,T);s.intw17(tspan,T);s.intw18(tspan,T);s.intw19(tspan,T);s.intw20(tspan,T);...
    s.intw21(tspan,T);s.intw22(tspan,T);s.intw23(tspan,T);s.intw24(tspan,T);s.intw25(tspan,T);...
    s.intw26(tspan,T);s.intw27(tspan,T);s.intw28(tspan,T);s.intw29(tspan,T);s.intw30(tspan,T);...
    s.intw31(tspan,T);s.intw32(tspan,T);s.intw33(tspan,T);s.intw34(tspan,T);s.intw35(tspan,T);...
    s.intw36(tspan,T);s.intw37(tspan,T);s.intw38(tspan,T);s.intw39(tspan,T);s.intw40(tspan,T);...
    s.intw41(tspan,T);s.intw42(tspan,T);s.intw43(tspan,T);s.intw44(tspan,T);s.intw45(tspan,T);...
    s.intw46(tspan,T);s.intw47(tspan,T);s.intw48(tspan,T);s.intw49(tspan,T);s.intw50(tspan,T)];
% solver of X
disp('solving X2')
tic
X2=zeros(1+betaK,num+1);

%the 1 term is 0

for ii=1:betaK
    %the xi term
    X2(1+ii,:)=intwvalue(ii,:);
end
toc
%varitest(tspan,(X2.*X2)',T,'uncorrelated','oudt',num)


%%
% WCE price
Npath=10000;
xi=randn(Npath,betaK);
xisquare = 1/sqrt(2)*(xi.^2-1);
xixi=etaietaj(xi);

price=zeros(51,1);
X2=-a*m*X2;



for iter=1:51
    asset=zeros(Npath,1);
    for ii=1:betaK
        asset=asset+X2(ii+1,iter)*xi(:,ii);
    end
    asset=asset+X2(1,iter);
    price(iter)=sum(abs(asset))/Npath;
end

disp('MC price')
tic
Npath=30000;
randseed =randn(5000,Npath);
sampleW= oupath(randseed,0,1,1,0);
%randseed =randn(5000,Npath); % because of uncorrelated
sampleX =sum(abs(midpoint(sampleW)),2);

mtclprice=sampleX/Npath;
toc

hold on
plot(time,price)
plot(time,mtclprice,'r')